package cn.zhangxin.pethome.system.verifycode.controller;

import cn.zhangxin.pethome.system.verifycode.domain.LogOnMessage;
import cn.zhangxin.pethome.system.verifycode.service.IVerifyCodeService;
import cn.zhangxin.pethome.utils.AjaxResult;
import cn.zhangxin.pethome.utils.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/vcode")
@Api(value = "验证码API",description="验证码验证")
public class VerifyContController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    //获得图片验证码
    @GetMapping("/getVCode/{imageCodeKey}")
    @ApiOperation(value = "获取验证码")
    public AjaxResult getVerificationCode(@PathVariable("imageCodeKey") String imageCodeKey) {
        String imgCode = verifyCodeService.getImgCode(imageCodeKey);
        return  AjaxResult.success().setData(imgCode);
    }

    //注册发送短信
    @PostMapping("/sendMessage")
    @ApiOperation(value = "注册发送短信")
    public AjaxResult sendMessage(@RequestBody LogOnMessage logOnMessage){

        try {
            verifyCodeService.sendMessage(logOnMessage);
            return AjaxResult.success();
        } catch (BusinessException businessException){
            return AjaxResult.error().setMsg(businessException.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
    //绑定发送短信
    @PostMapping("/sendSmsCodeBind")
    @ApiOperation(value = "绑定发送短信")
    public AjaxResult sendSmsCodeBind(@RequestBody LogOnMessage logOnMessage){

        try {
            verifyCodeService.sendSmsCodeBind(logOnMessage);
            return AjaxResult.success();
        } catch (BusinessException businessException){
            return AjaxResult.error().setMsg(businessException.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

}
